package com.example.testserviceaidlclient;

import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.os.RemoteException;
import android.view.View;

import androidx.appcompat.app.AppCompatActivity;

import com.example.testserviceaidl.IRemoteService;

public class MainActivity extends AppCompatActivity implements View.OnClickListener{

    Boolean mBound = false;
    IRemoteService iRemoteService;
    ServiceConnection mConn = new ServiceConnection() {
        @Override
        public void onServiceConnected(ComponentName name, IBinder service) { //service is binder
            // Following the example above for an AIDL interface,
            // this gets an instance of the IRemoteInterface, which we can use to call on the service
            LogUtil.printLog("onServiceConnected remote");
            iRemoteService = IRemoteService.Stub.asInterface(service);
            mBound = true;
        }

        @Override
        public void onServiceDisconnected(ComponentName name) {
            LogUtil.printLog("onServiceDisconnected remote");
            iRemoteService = null;
            mBound = false;
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        findViewById(R.id.id_btn_bind).setOnClickListener(this);
        findViewById(R.id.id_btn_unbind).setOnClickListener(this);
        findViewById(R.id.id_btn_servicecall).setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.id_btn_bind:
                //    public abstract boolean bindService(@RequiresPermission Intent service,
                //            @NonNull ServiceConnection conn, @BindServiceFlags int flags);
                if (!mBound) {
                    //intent.setPackage("com.status.aidlproject");
                    Intent intent = new Intent("android.intent.action.MyRemoteService");
                    intent.setPackage("com.example.testserviceaidl");
                    bindService(intent, mConn, Context.BIND_AUTO_CREATE);
                    mBound = true;
                }
                break;
            case R.id.id_btn_unbind:
                if (mBound) {
                    unbindService(mConn);
                    mBound = false;
                }
                break;
            case R.id.id_btn_servicecall:
                if (mBound) {
                    if (mBound) {
                        int pid = 0;
                        try {
                            pid = iRemoteService.getPid();
                            iRemoteService.getRandomNumber();
                        } catch (RemoteException e) {
                            e.printStackTrace();
                        }
                        LogUtil.printLog("MyService.getRandomNumber pid:" + pid);
                    }
                }
                break;
        }
    }
}
